package com.my.demos.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: 
 *      TTL队列 配置文件类代码
 * @date 2023/12/1 10:43
 */
@Configuration
public class TTLQueueConfig {
    
    // 普通交换机的名称
    public static final String X_EXCHANGE = "X";
    // 死信交换机名称
    public static final String Y_Dead_LETTER_EXCHANGE = "Y";
    // 普通队列名称
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    
    public static final String QUEUE_C = "QC";
    // 死信队列名称
    public static final String Dead_LETTER_QUEUE_D = "QD";
    
    // 声明xExchange
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    // 声明YExchange
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_Dead_LETTER_EXCHANGE);
    }
    
    // 声明队列
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> arguments = new HashMap<>(3);
        // 配置死信交换机
        arguments.put("x-dead-letter-exchange", Y_Dead_LETTER_EXCHANGE);
        // 配置死信routingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        // 配置TTL 单位是ms
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    // 声明队列
    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> arguments = new HashMap<>(3);
        // 配置死信交换机
        arguments.put("x-dead-letter-exchange", Y_Dead_LETTER_EXCHANGE);
        // 配置死信routingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        // 配置TTL 单位是ms
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }
    
    @Bean("queueC")
    public Queue queueC(){
        Map<String, Object> arguments = new HashMap<>(3);
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_Dead_LETTER_EXCHANGE);
        // 设置死信routingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }
    
    // 声明死信队列
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(Dead_LETTER_QUEUE_D).build();
    }
    
    // 绑定QueueA
    @Bean
    public Binding queueABindingX(Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }
    
    // 绑定QueueB
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }
    
    // 绑定QueueC
    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }
    
    // 绑定死信队列QueueD
    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
    
}
